<template>
    <div class="search">
        <input v-model="searchVal" type="text" placeholder="请输入关键字">
        <ul class="mui-table-view">
            <li class="mui-table-view-cell mui-media" v-for="(item,index) in list" :key="item.id">
                <router-link :to="'/home/GoodsInfo/' + item.id">
                    <img class="mui-media-object mui-pull-left" :src="item.url">
                    <div class="mui-media-body">
                        <h4>{{item.title}}</h4>
                        <p class='mui-ellipsis'>
                            <span>发表时间：{{item.add_time | dataFormat('YYYY-MM-DD')}}</span>
                            <span>点击：{{item.click}}次</span>
                        </p>
                    </div>
                </router-link>
            </li>
        </ul>
        <p v-show="resultShow">没有你要找的商品</p>
    </div>
</template>
<script>
    export default{
        data(){
            return {
                goodsList:[],
                searchGood:[],
                searchVal:'',    //默认输入为空
                resultShow:false

            }
        },
        created(){
            this.getSearchGood();
        },
        methods: {
            getSearchGood(){
                this.$http.get('src/mock/goodsList.json').then(result=>{
                    if (result.status === 200){
                        this.goodsList = result.body;
                    }
                })
            }
        },
        watch:{

        },
        computed:{
            list: function(){
                /*方法一*/
                //逻辑-->根据input的value值筛选goodsList中的数据
                var arrByZM = [];//声明一个空数组来存放数据
                for (var i=0;i<this.goodsList.length;i++){
                    //for循环数据中的每一项（根据title值）
                    if(this.goodsList[i].title.search(this.searchVal) != -1){
                        //判断输入框中的值是否可以匹配到数据，如果匹配成功
                        arrByZM.push(this.goodsList[i]);
                        this.resultShow = false;
                        //向空数组中添加数据
                    }

                    if(arrByZM == ''){
                        this.resultShow = true;
//                        return false;
                    }
                }



                return arrByZM;


                /*方法二*/
//                var search = this.searchVal;
//
//                if (search) {
//                    return this.goodsList.filter(function(product) {
//                        return Object.keys(product).some(function(key) {
//                            return String(product[key]).toLowerCase().indexOf(search) > -1
//                        })
//                    })
//                }
//
//                return this.goodsList;

            }
        },
        components: {}
    }
</script>
<style lang="scss" scoped>
    ul,li{
        list-style: none;
    }
.search{
    padding: 20px 5px;
}

</style>